package com.xyzwps.paimon.katheryne.modules.user;

import com.xyzwps.paimon.katheryne.commons.DB;
import io.helidon.http.Status;
import io.helidon.webserver.http.HttpRules;
import io.helidon.webserver.http.HttpService;
import io.helidon.webserver.http.ServerRequest;
import io.helidon.webserver.http.ServerResponse;

public class UserRouter implements HttpService {

    @Override
    public void routing(HttpRules rules) {
        rules.get("/{id}", this::getById);
    }

    private void getById(ServerRequest req, ServerResponse resp) {
        var userId = Long.parseLong(req.path().pathParameters().get("id"));

        var tx = DB.client.transaction();
        var accountRepo = new UserAccountRepository(tx);
        var $account = accountRepo.findByUserId(userId);
        tx.commit();

        $account.ifPresentOrElse(
                account -> {
                    resp.status(Status.OK_200).send(account);
                }, () -> {
                    resp.status(Status.BAD_REQUEST_400).send();
                });
    }
}
